//
//  TXPRadioPageViewController.m
//  StayAwhile
//
//  Created by dllo on 16/1/27.
//  Copyright © 2016年 dllo. All rights reserved.
//

#import "TXPRadioPageViewController.h"
#import "TXPRadioPageView.h"
#import "TXPRadioModel.h"
#import "TXP_RadioTableViewCell.h"
#import "TXP_RadioHotlistTableViewCell.h"
#import "TXPRadioDetailViewController.h"
#import <MJRefresh.h>
@interface TXPRadioPageViewController ()<UITableViewDataSource,UITableViewDelegate,TXPRadioPageViewDelegate,TXP_RadioHotlistTableViewCellDelegate>
@property (nonatomic,strong)UITableView *radioTableView;
@property (nonatomic,strong)NSMutableArray *carouselArray;
@property (nonatomic,strong)NSMutableArray *alllist;
@property (nonatomic,strong)NSMutableArray *hotlistArray;
@property(nonatomic,assign)NSInteger start;//记录下一页
@property(nonatomic,assign)BOOL isUpLoading;//标记上拉操作还是下拉操作，yes为上拉
@property (nonatomic, assign) int flag; //记录当前翻到了第几页
@end

@implementation TXPRadioPageViewController

-(void)viewWillAppear:(BOOL)animated{
    
    [super viewWillAppear:animated];
    self.navigationController.navigationBar.hidden=NO;
}

-(void)viewWillDisappear:(BOOL)animated{
    [super viewWillAppear:animated];
    self.navigationController.navigationBar.hidden=YES;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
  self.navigationItem.title = @"电台";
    self.view.backgroundColor = [UIColor yellowColor];
    self.radioTableView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStyleGrouped];
    self.radioTableView.delegate = self;
    self.radioTableView.dataSource = self;
    [self.radioTableView registerClass:[TXP_RadioTableViewCell class] forCellReuseIdentifier:@"alllist"];
    [self.radioTableView registerClass:[TXP_RadioHotlistTableViewCell class] forCellReuseIdentifier:@"hotlist"];
    self.isUpLoading = NO;
       MJRefreshGifHeader *header = [MJRefreshGifHeader headerWithRefreshingTarget:self refreshingAction:@selector(addHeader)];
    [header beginRefreshing];
    [header setImages:nil forState:MJRefreshStateRefreshing];
    self.radioTableView.mj_header = header;
    self.radioTableView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
        // 进入刷新状态后会自动调用这个block
        [self addFooter];
    }];
    self.radioTableView.mj_footer.automaticallyHidden = NO;
    [self.view addSubview:self.radioTableView];
}
- (void)addHeader{
    
    self.start = 0;
    self.isUpLoading = NO;
    [self PostWithRadioData:self.start];
}
- (void)addFooter{
    
    self.start+=9;
    self.isUpLoading = YES;
    [self PostFooterRadioData:self.start];
    
}
- (NSMutableArray *)hotlistArray
{
    if (!_hotlistArray) {
        _hotlistArray = [NSMutableArray array];
    }
    return _hotlistArray;
}
- (NSMutableArray *)carouselArray
{
    if (!_carouselArray ) {
        
        _carouselArray = [NSMutableArray array];
    
    }
    return _carouselArray;
}
- (NSMutableArray *)alllist{
    
    if (!_alllist) {
        _alllist = [NSMutableArray array];
    }
    return _alllist;
}
#pragma mark - 数据解析
- (void)PostWithRadioData:(NSInteger )str{
    
    [TXPAFNetworkingBlock PostAFNURLWithString:@"http://api2.pianke.me/ting/radio" Body:@{ @"client":@"2"} Cookie:nil resultBlock:^(id result) {
        
        NSLog(@"result%@",result);
        NSMutableDictionary *dic = [result objectForKey:@"data"];
        NSLog(@"%@",dic);
        NSMutableArray *carouselArray = [dic objectForKey:@"carousel"];
        if (self.isUpLoading==NO) {
            [self.alllist removeAllObjects];
            [self.carouselArray removeAllObjects];
            [self.hotlistArray removeAllObjects];
            
        }
        for (NSDictionary *dic in carouselArray) {
            TXPRadioModel *radioModel = [TXPRadioModel modelWithDictionary:dic];
            [self.carouselArray addObject:radioModel];
            NSLog(@"%@",self.carouselArray);
        }
        NSMutableArray *alllistArray = [dic objectForKey:@"alllist"];
        
        for (NSDictionary *dic in alllistArray) {
            TXPRadioModel *radioModel = [TXPRadioModel modelWithDictionary:dic];
            [self.alllist addObject:radioModel];
        }
        
        NSMutableArray *hotlistArray = [dic objectForKey:@"hotlist"];
        for (NSDictionary *dic in hotlistArray) {
            TXPRadioModel *model = [TXPRadioModel modelWithDictionary:dic];
            [self.hotlistArray addObject:model];
        }
        [self.radioTableView.mj_header endRefreshing];
        [self.radioTableView.mj_footer endRefreshing];
        [self.radioTableView reloadData];
    } errorBlock:^(NSError *error) {
        
 
    }];
}
- (void)PostFooterRadioData:(NSInteger)str{
    
    
    [TXPAFNetworkingBlock PostAFNURLWithString:@"http://api2.pianke.me/ting/radio_list" Body:@{ @"client":@"2",@"limit":@"9", @"start" : [NSString stringWithFormat:@"%ld",(long)str]} Cookie:nil resultBlock:^(id result) {
        
        NSMutableDictionary *dic = [result objectForKey:@"data"];
        NSLog(@"%@",dic);

       
        NSMutableArray *alllistArray = [dic objectForKey:@"list"];
        
        for (NSDictionary *dic in alllistArray) {
            TXPRadioModel *radioModel = [TXPRadioModel modelWithDictionary:dic];
            [self.alllist addObject:radioModel];
        }
        
        [self.radioTableView.mj_header endRefreshing];
        [self.radioTableView.mj_footer endRefreshing];
        [self.radioTableView reloadData];
    } errorBlock:^(NSError *error) {
        
        
    }];
    
    
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    
    return 180;
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    
    NSMutableArray *array = [NSMutableArray array];
    
    for (TXPRadioModel *model in self.carouselArray) {
        [array addObject:model.img];
    }
    TXPRadioPageView *header = [[TXPRadioPageView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, 180)];
    header.allImgArray = [NSMutableArray arrayWithArray:array];
    header.myDelegate = self;
    return header;
    
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return self.alllist.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
       if (indexPath.row==0) {

        TXP_RadioHotlistTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"hotlist"];
           cell.imgArray = self.hotlistArray;
           cell.myDelegate = self;
        return cell;
    }else{
        
        TXP_RadioTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"alllist"];
        TXPRadioModel *model = self.alllist[indexPath.row-1];
        cell.model = model;
        return cell;
    }

}
- (void)TXPImgView:(TXPRadioPageView *)imgeView didSelectIndex:(NSInteger)pageIndex
{
    TXPRadioModel *model = self.carouselArray[pageIndex];
    NSString *url = [model.url substringWithRange:NSMakeRange(9, 2)];
    NSLog(@"url%@",url);
    if ([url isEqualToString:@"fm"]) {
        NSLog(@"pageIndex%ld",pageIndex);
        TXPRadioDetailViewController *radioDetail = [[TXPRadioDetailViewController alloc] init];
        radioDetail.model = model;
        [self.navigationController pushViewController:radioDetail animated:YES];
    }else{
        
        NSLog(@"放歌");
    }

}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return HEIGHT * 0.15;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.row!=0) {
        TXPRadioDetailViewController *radioDetail = [[TXPRadioDetailViewController alloc] init];
        radioDetail.model = self.alllist[indexPath.row-1];
        [self.navigationController pushViewController:radioDetail animated:YES];
    }
   
}
- (void)pushDetail:(TXPRadioModel *)model
{
    TXPRadioDetailViewController *radioDetail = [[TXPRadioDetailViewController alloc] init];
    radioDetail.model = model;
    [self.navigationController pushViewController:radioDetail animated:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
